<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;


class Active extends Model
{

    use SoftDelete;
    // 表名
    protected $name = 'active';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'delete_time';

    const STATUS_UP = 1; // 上架
    const STATUS_DOWN = 0; // 下架

    public static function getStatusArray(){
        return [
            self::STATUS_UP     => '上架',
            self::STATUS_DOWN   => '下架',
        ];
    }
    // 追加属性
    protected $append = [
        'new_up_time_text',
        'new_down_time_text',
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
//            $pk = $row->getPk();
//            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getNewUpTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['new_up_time']) ? $data['new_up_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getNewDownTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['new_down_time']) ? $data['new_down_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function shop()
    {
        return $this->belongsTo('Shop', 'shop_id', 'id')->setEagerlyType(0);
    }

}
